package com.edu.chapter07;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.edu.chapter07.bean.User;
import com.edu.chapter07.network.OkHttpHelper;
import com.edu.chapter07.network.RetrofitHelper;
import com.edu.chapter07.network.RetrofitNetworkHelper;

import retrofit2.Retrofit;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    MyTextView textView;
    TextView textView2;
    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_request).setOnClickListener(this);
        findViewById(R.id.btn_request2).setOnClickListener(this);
        textView = (MyTextView) findViewById(R.id.tv_response);
        textView2 = findViewById(R.id.tv_response2);
        findViewById(R.id.btn_clear).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (R.id.btn_request == v.getId()) {
            new OkHttpHelper().searchGames(response -> {
                // 处理网络返回的用户数据
                textView.setText("");
                textView.append(response.getId() + "\n");
                textView.append(response.getName() + "\n");
                textView.append(response.getPhone() + "\n");
            });
        } else if (R.id.btn_request2 == v.getId()) {
            new RetrofitNetworkHelper().fetchUser(new RetrofitNetworkHelper.ResponseCallback() {
                @Override
                public void onSuccess(User user) {
                    textView2.setText("");
                    textView2.append(user.getId() + "\n");
                    textView2.append(user.getName() + "\n");
                    textView2.append(user.getPhone() + "\n");
                }

                @Override
                public void onFailure(String errorMsg) {
                }
            });
        } else if (R.id.btn_clear == v.getId()) {
            textView2.setText("");
            textView.setText("");
        }
    }
}